package org.jeecg.modules.demo.weixin.smtsharingmember.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.weixin.entity.SmtSharingMember;
import org.jeecg.modules.demo.weixin.smtsharingmember.mapper.SmtSharingMemberMapper;
import org.jeecg.modules.demo.weixin.smtsharingmember.service.ISmtSharingMemberService;
import org.jeecg.modules.demo.weixin.vo.ProfitVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 分账记录表
 * @Author: jeecg-boot
 * @Date: 2023-11-21
 * @Version: V1.0
 */
@Service
public class SmtSharingMemberServiceImpl extends ServiceImpl<SmtSharingMemberMapper, SmtSharingMember> implements ISmtSharingMemberService {

    @Override
    public void updateState(SmtSharingMember smtSharingMember, JSONObject jsonObject) {
        //获取每一个分账接收方对应的结果
        List<ProfitVo> profitVos = jsonObject.getJSONArray("receivers").stream().map(item -> JSON.parseObject(((JSONObject) item).toString(), ProfitVo.class)).collect(Collectors.toList());
        profitVos.forEach(profitVo -> {
//            LambdaQueryWrapper<SmtSharingMember> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(SmtSharingMember::getTransactionId, smtSharingMember.getTransactionId());
//            queryWrapper.eq(SmtSharingMember::getDetailId, profitVo.getDetailId());
//            SmtSharingMember sharingMember = this.getOne(queryWrapper);
//            if (sharingMember != null) {
//                sharingMember.setState(profitVo.getResult());
//                sharingMember.setFailReason(profitVo.getFailReason());
//                this.updateById(sharingMember);
//            }
            UpdateWrapper<SmtSharingMember> wrapper = new UpdateWrapper<>();
            wrapper.eq("detail_id", profitVo.getDetailId());
            wrapper.eq("out_order_no", smtSharingMember.getOutOrderNo()  );
            wrapper.set("state", profitVo.getResult());
            wrapper.set(profitVo.getFailReason()!=null, "fail_reason", profitVo.getFailReason());
            this.update(wrapper);
        });
    }
}
